package com.tanhua.server.service;

import com.tanhua.autoconfig.templates.HuanXinTemplate;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.ContactVo;
import com.tanhua.vo.MessageLike;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {
    //commentType，1-点赞，2-评论，3-喜欢

    @Reference
    private CommentApi commmentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private FriendApi friendApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity queryComment(int commentType, Integer page, Integer pagesize) {

//        查询对当前登录人的点赞、喜欢、评论
     PageResult pageResult = commmentApi.findByPublishUserId(UserHolder.getUserId(),commentType,page,pagesize);

     List<Comment> items = (List<Comment>) pageResult.getItems();

     List<MessageLike> list = new ArrayList<>();

        for (Comment comment : items) {
            MessageLike messageLike = new MessageLike();
            Long userId = comment.getUserId();
            UserInfo userInfo = userInfoApi.findById(userId);
            if(userInfo==null){
                continue;
            }
            messageLike.setAvatar(userInfo.getAvatar());
            messageLike.setId(userInfo.getId().toString());
            messageLike.setNickname(userInfo.getNickname());
            messageLike.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm")); //评论时间   2019-09-08 10:07
            list.add(messageLike);
        }

        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }



    public ResponseEntity saveContacts(Long friendId) {
        Long userId = UserHolder.getUserId();
//        1、记录到MongoDB中
        friendApi.save(userId,friendId);
//      2、记录到环信中
        huanXinTemplate.contactUsers(userId,friendId);

        return ResponseEntity.ok(null);

    }



    public ResponseEntity contactsList(Integer page, Integer pagesize, String keyword) {
      PageResult pageResult =  friendApi.contactsListByUserId(page,pagesize,UserHolder.getUserId());
        List<Friend> items = (List<Friend>) pageResult.getItems();

        List<ContactVo> list = new ArrayList<>();
        for (Friend item : items) {
            Long friendId = item.getFriendId();
            UserInfo userInfo = userInfoApi.findById(friendId);
            if(userInfo==null){
                continue;
            }
            ContactVo contactVo = new ContactVo();
            BeanUtils.copyProperties(userInfo,contactVo);
            contactVo.setUserId(userInfo.getId().toString());
//            userInfo中的city是 "北京市-北京城区-东城区"  contactVo只要“北京城区”
            if(userInfo.getCity()!=null){
                contactVo.setCity(userInfo.getCity().split("-")[1]);
            }
            list.add(contactVo);
        }

        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);

    }
}
